// riscv64其实跟x86_64是一样一样的
// 发现一个问题，我不小心把lw写成ld，会报段错误，按理说不应该报错，只是数据不正确而已，到底什么原因，待探索
.text
.section .rodata
.align 3
.LC0:
    .string "Level %d: n location %p\n"

.text
.globl main
.type main, @function
main:
    addi sp, sp, -16
    sd ra, 8(sp)
    sd s0, 0(sp)
    li a0, 1
    call up_and_down
    li a0, 0
    ld ra, 8(sp)
    ld s0, 0(sp)
    addi sp, sp, 16
    jr ra

.text
.globl up_and_down
.type up_and_down, @function
up_and_down:
    addi sp, sp, -32
    sd ra, 24(sp)
    sd s0, 16(sp)
    addi s0, sp, 32
    // -20(s0)
    sw a0, -20(s0)

    // &n
    addi a2, s0, -20
    // n
    lw a1, -20(s0)
    lui a5, %hi(.LC0)
    addi a0, a5, %lo(.LC0)
    call printf

    lw a5, -20(s0)
    li a4, 4
    bge a5, a4, .L2
.L1:
    lw a0, -20(s0)
    addiw a0, a0, 1
    sext.w a0, a0
    call up_and_down

.L2:

    // &n
    addi a2, s0, -20
    // n
    lw a1, -20(s0)
    lui a5, %hi(.LC0)
    addi a0, a5, %lo(.LC0)
    call printf

    ld ra, 24(sp)
    ld s0, 16(sp)
    addi sp, sp, 32
    jr ra
    